//
//  LoginViewController.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UserServer.h"
#import "UserAgreementViewController.h"

@interface LoginViewController ()


@end

@implementation LoginViewController

@synthesize loginView = _loginView;



#pragma mark - get
-(UIBarButtonItem *)leftBtnItem
{
    if (!_leftBtnItem) {
        _leftBtnItem = [[UIBarButtonItem alloc] initWithTitle:KNSLocal(@"alertCancel") style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
        [_leftBtnItem setTitleTextAttributes:@{NSFontAttributeName:PFR_FONT(16), NSForegroundColorAttributeName :TINT_GREEN_COLOR} forState:UIControlStateNormal];
    }
    return _leftBtnItem;
}

-(CustomLoginView *)loginView
{
    if (!_loginView) {
        _loginView = [[CustomLoginView alloc] initWithFrame:self.view.frame withType:Type_Login];
        _loginView.delegate = self;
    }
    return _loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initWithUserInterface];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.loginView showKeyBoard];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.loginView hiddenKeyBoard];
}

-(void)initWithUserInterface
{
    //状态栏
    [UITool setStatusBarIsWhite:NO];
    
    self.navigationItem.leftBarButtonItem = self.leftBtnItem;
    
    [self.view addSubview:self.loginView];
    
    [self.loginView verifyTetxFieldContent];
}

#pragma mark - delegate
-(void)LoginViewProcessBtn:(NSInteger)btnIndex
{
    switch (btnIndex) {
        case 0:
        {
            [self gotoLogin];
        }
            break;
        case 1:
        {
            [self gotoRegistered];
        }
            break;
            
        default:
            break;
    }
}
#pragma mark - 点击用户协议
-(void)processUserAgreemnntBtn
{
    UserAgreementViewController * userAgreementVC = [[UserAgreementViewController alloc] init];
    [self.navigationController pushViewController:userAgreementVC animated:YES];
}

#pragma mark - 登陆
-(void)gotoLogin
{
    [self showLoadingHudWithHText:nil];
    
    ContactModel * userContact = [ContactModel new];
    userContact.userName = self.loginView.username;
    userContact.password = self.loginView.password;
    //暂时先不MD5加密
    @weakify(self);
    [[UserServer shared] LoginWithUser:userContact complete:^(BOOL succees, NSString *error, ContactModel *contact) {
        @strongify(self);
        [self hiddenLoadingHud];
        if (succees) {
            [UserInfoManger shared].isLogin = YES;
            [self cancel];
        }else{
            [self sureAlertWithTitle:nil andMesg:error];
        }
    }];
}

#pragma mark - 注册
-(void)gotoRegistered
{
    RegisterViewController * registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

#pragma mark - 取消
-(void)cancel
{
    [self.loginView hiddenKeyBoard];
    //状态栏
    [UITool setStatusBarIsWhite:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
